package com.jxnu.escms.Control.UserControl;

import com.jxnu.escms.Pojo.User;
import com.jxnu.escms.Service.UserService.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static java.util.regex.Pattern.compile;

@Controller
public class UserControl {
    @Resource
    private UserService userService;

    /**
     * @param input 账号/邮箱，密码
     * @param session
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public Map login(@RequestBody Map<String, Object> input,
                     HttpSession session){
        Map<String, Object> map = new HashMap<>();
        String input1 = (String) input.get("input");
        String password = (String) input.get("password");
        User user1 = userService.containUserByNameAndPassword(input1, password);
        if(user1 != null){
            map.put("login", "success");
            updateDate(user1.getId());
            // 获取用户地址
            String address = getAddress(user1, user1.getId());
            user1.setAddress(address);
            map.put("user", user1);
            return map;
        }
        else{
            user1 = userService.containUserByEmailAndPassword(input1, password);
            if(user1 != null){
                map.put("login", "success");
                map.put("user", user1);
                updateDate(user1.getId());
                // 获取用户地址
                String address = getAddress(user1, user1.getId());
                user1.setAddress(address);
                map.put("user", user1);
                return map;
            }
        }
        map.put("login", "worry");
        map.put("result", "用户不存在");
        return map;
    }

    /**
     * @param input 邮箱，用户名，密码
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public Map register(@RequestBody Map<String, String> input){
        Map<String, Object> res = new HashMap<>();
        String email = input.get("email");
        String username =input.get("username");
        String password = input.get("password");
        // 首先判断是否存在此用户
        User user1 = userService.containUserByNameAndEmail(username, email);
        if(user1 != null){
            res.put("register", "worry");
            res.put("result", "用户已存在，不可重复注册！！！");
        }
        else{
            // 用户不存在，注册此用户
            int i = userService.registerUser(input);
            if(i == 1){
                res.put("register", "success");
                res.put("result", "注册成功！！！");
            }
            else{
                res.put("register", "worry");
                res.put("result", "注册失败！！！");
            }
        }
        return res;
    }

    @RequestMapping("/changeMes")
    @ResponseBody
    public Map changeUserMes(@RequestBody User user){
        // 根据用户id从数据库获取用户信息
        User userByID = userService.getUserByID(user.getId());

        if(Objects.equals(user.getPassword(), "****")){
            user.setPassword(userByID.getPassword());
        }
        user.setSex(Objects.equals(user.getSex(), "1") ? "男":"女");
        // 更新用户信息
        userService.changeUserMes(user);

        // 获取用户地址
        String address = getAddress(user, user.getId());
        if(!Objects.equals(address, user.getAddress())){
            // 更新用户地址
            List addr = parseAddress(user.getAddress());
            userService.updateUserAddress(addr, user.getId());
        }
        Map res = new HashMap();
        res.put("update", "success");
        return res;
    }


    /**
     * @param id 用户ID
     */
    private void updateDate(int id){
        // 当用户登入成功时，更新登入时间
        SimpleDateFormat simpleFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = simpleFormatter.format(new Date());
        userService.updateDate(date, id);
    }

    public String getAddress(User user, int id){
        Map<String, Object> userAddress = null;
        if(user != null){
            userAddress = userService.getUserAddress(user.getId());
        }
        else{
            userAddress = userService.getUserAddress(id);
        }
        if(userAddress != null){
//            "address": {
//                "nuit": "西",
//                        "user_id": 2,
//                        "building": "10",
//                        "doorplate": "331",
//                        "com_name": "昌九社区"
//            }
            String com = (String) userAddress.get("com_name");
            String door = (String) userAddress.get("doorplate");
            String build = (String) userAddress.get("building");
            String nuit = (String) userAddress.get("nuit");
            return com + build + "楼" + nuit + "单元" + door + "门牌号";
        }
        return null;
    }

    public List parseAddress(String address){
        String addr = address;

        Pattern compile1 = compile("(.*)社区");
        Matcher mather1 = compile1.matcher(addr);
        String com ="";
        while(mather1.find()){
            com = mather1.group();
        }

        Pattern compile2 = compile("(.*)楼");
        String addr2 = addr.replace(com, "");
        Matcher mather2 = compile2.matcher(addr2);
        String  build = "";
        while(mather2.find()){
            build = mather2.group();
        }

        Pattern compile3 = compile("(.*)单元");
        String addr3 = addr2.replace(build, "");
        Matcher mather3 = compile3.matcher(addr3);
        String  nuit = "";
        while(mather3.find()){
            nuit = mather3.group();
        }

        Pattern compile4 = compile("(.*)门牌号");
        String addr4 = addr3.replace(nuit, "");
        Matcher mather4 = compile4.matcher(addr4);
        String  door = "";
        while(mather4.find()){
            door = mather4.group();
        }

        build = build.replace("楼", "");
        nuit = nuit.replace("单元", "");
        door = door.replace("门牌号", "");

        List<Object> addrList = new ArrayList<>();
        addrList.add(com);
        addrList.add(build);
        addrList.add(nuit);
        addrList.add(door);
        return addrList;
    }
}
